#pragma once

#include "Simples.h"
#include <functional>

class BasicCatAnalysis
{
    typedef std::function<void (unsigned int index, DATA_GRAM &item)> ItemCallBackFunction;
public:
    BasicCatAnalysis(void);
    virtual ~BasicCatAnalysis(void);

    void SetItems(const std::map<unsigned int, DATA_GRAM> &items);

    bool DataParser(const char *data, unsigned int length);
    void ResetItems();
    void ClearItems();
    CAT_SIMPLE & GetDataBlock();

    void AddItemData(unsigned int index, Value item_value);
    void DisplayString(const std::string&str);

    unsigned int FindItemIndex(const std::string &item_num);

    std::string Encoder();

    std::string GetIntBytesBigIndian(int val, unsigned char len = 4);

    virtual std::string OriginalPackMessage(const CatRadarData &data) { return ""; }
protected:
    virtual bool IsCatRight(unsigned char) { return true; }
    virtual void InitDataGram() {}
    virtual void SetItemData(unsigned int key, DATA_GRAM& item) {}

    void ValueToItemData();
    unsigned int GetData4Bytes(const char *data);
    unsigned int GetData3Bytes(const char *data);
    unsigned short GetData2Bytes(const char *data);

    void InitDataGramData(DATA_GRAM &item, const std::string &item_name, 
        const std::string & item_num, unsigned int length,  _DATA_TYPE_ data_type = DATA_TYPE_FIX_LENGTH);

    void InitDataGramData(DATA_GRAM *item, const std::string &item_name,
        const std::string & item_num, unsigned int length,  _DATA_TYPE_ data_type = DATA_TYPE_FIX_LENGTH);
private:
    bool IsMoreFspec(byte temp);
    uint GetFspecLength(const char *data);
    void DataGramParser(const char *data, unsigned int fspeclen);
    unsigned int GetSerialNum(unsigned int count, unsigned char index);
    void GetSerialNum(unsigned int frn, unsigned int &count, unsigned char &index);
    void SetDataGramSourceData(DATA_GRAM & item, const char *data, unsigned int &offset);
    void GetFixDataByLength(DATA_GRAM& item, const char *data, unsigned int &offset, unsigned int visual_len = 0);

    void ForEachItem(ItemCallBackFunction);
    void DisplayItemInfo(unsigned int index, const DATA_GRAM &item);
protected:
    CAT_SIMPLE datablock_;
};
